package com.homework;

/**
 * @Author MDD
 * @Description
 * @Version
 * @Date
 **/
//1) 定义接口Comparable，其中包含唯一的方法int compareTo(Object obj)；
//2) 定义学生类，包括学号、姓名、年龄和分数，实现Comparable接口，按照分数倒序排列；
//3) 定义新闻类，包括编号（int类型）、标题、内容和点击数，实现Comparable接口，按照编号正序排列；
//4) 定义测试类，分别创建两个学生对象、新闻对象，进行比较并输出结果。
// 返回值>0，表示大于，返回值=0，表示等于，返回值<0，表示小于
public class ComparableTest {
    public static void main(String[] args) {
        Student stu1 = new Student(10011,"小明",18,89.5);
        Student stu2 = new Student(10021,"小红",19,90);
        if (stu1.compareTo(stu2)>0){
            System.out.println(stu2);
            System.out.println(stu1);
        }else {
            System.out.println(stu1);
            System.out.println(stu2);
        }
        System.out.println("---------------------------------------");
        Paper p1 = new Paper(001,"中国夺冠","台球","99999999");
        Paper p2 = new Paper(002,"震撼！！！！","中国阅兵","999999999999");
        if (p1.compareTo(p2)>0){
            System.out.println(p2);
            System.out.println(p1);
        }else {
            System.out.println(p1);
            System.out.println(p2);
        }
    }

}

interface Comparable{
    public int compareTo(Object obj);
}

class Student implements Comparable{
    int id;
    String name;
    int age;
    double score;

    @Override
    public int compareTo(Object obj) {
        Student oth = (Student)obj;
        if (this.score>oth.score){
            return 1;
        }else if (this.score<oth.score){
            return -1;
        }else{
            return 0;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    public Student() {
        super();
    }

    public Student(int id, String name, int age, double score) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getScore() {
        return this.score;
    }

    public void setScore(double score) {
        this.score = score;
    }


}

class Paper implements Comparable{
    int No;
    String title;
    String news;
    String lick;

    @Override
    public int compareTo(Object obj) {
        Paper oth = (Paper) obj;
        return this.No - oth.No;
    }

    @Override
    public String toString() {
        return "Paper{" +
                "No=" + No +
                ", title='" + title + '\'' +
                ", news='" + news + '\'' +
                ", lick='" + lick + '\'' +
                '}';
    }

    public Paper(int no, String title, String news, String lick) {
        this.No = no;
        this.title = title;
        this.news = news;
        this.lick = lick;
    }

    public int getNo() {
        return this.No;
    }

    public void setNo(int no) {
        this.No = no;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getNews() {
        return this.news;
    }

    public void setNews(String news) {
        this.news = news;
    }

    public String getLick() {
        return this.lick;
    }

    public void setLick(String lick) {
        this.lick = lick;
    }

    public Paper() {
        super();
        
    }


}